package cn.good.yan.controller;

import cn.good.yan.mapper.BillMapper;
import cn.good.yan.pojo.Bill;
import cn.good.yan.resp.ResultData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author shengren.yan
 * @create 2022-12-04
 */
@RestController
@Slf4j
@Tag(name = "服务模块", description = "支付bill")
public class BillController {
    @Resource
    private BillMapper billMapper;

    // post 请求
    @PostMapping(value = "/pay/add")
    @Operation(summary = "新增", description = "新增支付流水方法,json串做参数")
    public ResultData<String> addPay(@RequestBody Bill bill) {
        System.out.println(bill.toString());
        int i = billMapper.add(bill);
        return ResultData.success("成功插入记录，返回值: " + i);
    }

    @DeleteMapping(value = "/pay/del/{id}")
    @Operation(summary = "删除", description = "删除支付流水方法")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id) {
        int i = billMapper.deletePByPid((long) id);
        return ResultData.success(i);
    }

    @PutMapping(value = "/pay/update")
    @Operation(summary = "修改", description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody Bill payDTO) {
        Bill pay = new Bill();
        BeanUtils.copyProperties(payDTO, pay);
        int i = billMapper.update(pay);
        return ResultData.success("成功修改记录，返回值: " + i);
    }

    @GetMapping(value = "/pay/get/{id}")
    @Operation(summary = "按照ID查流水", description = "查询支付流水方法")
    public ResultData<Bill> getById(@PathVariable("id") Integer id) {
        if (id == -4) {
            throw new RuntimeException("id不能为负数");
        }
        // 暂停62秒钟线程,故意写bug，测试出feign的默认调用超时时间
//        try {
//            TimeUnit.SECONDS.sleep(62);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        List<Bill> pay = billMapper.findByBid((long) id);
        return ResultData.success(pay.get(0));
    }


}
